08. Spring Boot Unit Test, Hot Swap And Logging

035ND C01 L03 A15 SPRINGBOOT

Example - instructions

In this example, we are going to create a spring boot application first, then create a unit test to test it.

  1. Create a new spring boot application name spring-boot-unit-tests.
  2. Add web as dependency. Then download, and import the project into IntelliJ
  3. Create a dao directory and create a UserDao class inside.
@Repository
public class UserDao {
   public String getUser() {
       return "Tom";
   }
}
  • Create a service directory and create a UserService class inside.
@Service
public class UserService {
   @Resource
   private UserDao userDao;

   public String getUser() {
       return userDao.getUser();
   }
}
  • Create a unit test class under test/java/com/example/springbootunittest called UserServiceTest with code
@RunWith(SpringJUnit4ClassRunner.class) // @RunWith: integrate spring with junit
@SpringBootTest(classes = {SpringBootUnitTestApplication.class}) // @SpringBootTest: this class is spring boot test.
public class UserServiceTest {
   @Resource
   private UserService userService;

   @Test
   public void testGetUser() {
       Assert.assertEquals(userService.getUser(), "Tom");
   }
}

Directly run the UserServiceTest class to see the test result.

035ND C01 L03 A16 CREATE SPRINGBOOT

If everything is setup correctly, you should see a test case passed. What we are doing here is, we are adding a one method class UserDao, and a business logc UserService. UserDao has @Repository and UserService uses @Service. And we created a Spring Boot Unit test to test the UserService.

It’s very similar to Junit test. In addition, we added annotation @RunWith(SpringJUnit4ClassRunner.class) to integrate junit with spring boot. And we added @SpringBootTest(classes = {SpringBootUnitTestApplication.class}) to tell the application that this class is spring boot test.

Since we are going to cover more Spring Boot test in future class, I just give you an idea about how spring boot unit test look like here. If you are interested to see how unit test works in spring boot, please feel free to create more unit tests on your own.

Spring Boot unit test annotations

035ND C01 L03 A18 ANNOTATIONS

Example - instructions

In this example, we are going to add the DevTool step by step. Instead of creating a new project, we can reuse our existing project spring-boot-thymeleaf. In your future project, you can directly add DevTool dependency when you creating your Spring Boot application.

  • Add the devtool dependency to your pom.xml
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>
  • You may want to make sure the following item are there before jumping into testing.

https://www.mkyong.com/spring-boot/intellij-idea-spring-boot-template-reload-is-not-working/, personally I found this extremely helpful.

035ND C01 L03 A17 SPRINGBOOT UNIT TEST EXAMPLE

Now you have the spring boot devtool setup. Let’s do some testing to make sure we got what we are expecting.

  1. We want to make sure it works for html pages http://localhost:8080/demo. Update “You passed the exam.” in your demo.html to “You successfully passed your exam.” Give a sec for the application server to restart and refresh the demo page.

  2. We also want to make sure it works for changes in Java. First goto localhost:8080/demo2, Change Tom’s age from 30 to 32. When you clicked the save button, you would notice the changes in your console. And you will see the application server is restarting. When you refresh the page, you will see Tom’s age is updated.

Spring boot unit test and hot swap

035ND C01 L03 A19 HOTSWAP

035ND C01 L03 A20 DEVTOOL

035ND C01 L03 A21 DEVTOOL EXAMPLE

035ND C01 L03 A22 HOTSWAP SUMMARY

Summary

035ND C01 L03 A23 SPRINGBOOT SUMMARY

Springboot Logging

035ND C01 L03 A24 LOGGING INTRO

Spring Boot supports Java Util Logging, Log4j2, and Logback. By default, it uses Logback. With these logging components, you can configure the console logging as well as file logging for you to quickly detect problems and the source of the problems. The default Spring Boot logging provides the following information.

  1. Date and time.
  2. Log level - INFO, ERROR, TRACE, WARN. how many logs do you want to see? Based on the level you set, Spring Boot will print the messages fit for that level. 3 For complete logging level, you can see here: https://logging.apache.org/log4j/2.0/manual/architecture.html
  3. Process ID
  4. Thread name.
  5. Logger name - the source of the class.
    Log message

By default, all logs will be print on console, not in file. But in reality, we always want to be able to trace historical logs, so we need to keep them in file.

Check out demo code from github: https://github.com/udacity/JDND/tree/master/demos/c1/spring-boot-logging

As mentioned above, there are two outputs you can setup, console log or file log. In here I am setting the error level to Debug for both. Console Log: by default, “INFO”, “ERROR” and “WARN” log messages will print in the log file. If you want to enable Debug level Log. Just add the following to your application.properties.

debug=true

File Log: You need to specific your log file path in application.properties

logging.level.root = DEBUG
logging.file = /var/tmp/mylog.log

Spring Boot Logging Quiz

Which LOG level is higher? (meaning will produce more loggings)

SOLUTION: INFO

035ND C01 L03 A25 LOGGING DEMO

035ND C01 L03 A26 LOGGING ANOTHER OPTION